﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCTest003.Models;
using MVC_seed.Data;

namespace MVC_seed.Controllers
{
    public class MusicStoreController : Controller
    {
        private MusicStoreDB db = new MusicStoreDB();

        // GET: MVCStore
        public ActionResult Index()
        {
            var strSql = "select * from Alumbs";

            var albnms = db.Albnms.Include(a => a.Genre);
            return View(albnms.ToList());
        }

        // GET: MVCStore/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Albnm albnm = db.Albnms.Find(id);
            if (albnm == null)
            {
                return HttpNotFound();
            }
            return View(albnm);
        }

        // GET: MVCStore/Create
        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name");
            return View();
        }

        // POST: MVCStore/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,GenreId,ArtisId,Title,Price,AlbumArtUrl")] Albnm albnm)
        {
            if (ModelState.IsValid)
            {
                db.Albnms.Add(albnm);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", albnm.GenreId);
            return View(albnm);
        }

        // GET: MVCStore/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Albnm albnm = db.Albnms.Find(id);
            if (albnm == null)
            {
                return HttpNotFound();
            }
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", albnm.GenreId);
            return View(albnm);
        }

        // POST: MVCStore/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,GenreId,ArtisId,Title,Price,AlbumArtUrl")] Albnm albnm)
        {
            if (ModelState.IsValid)
            {
                db.Entry(albnm).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(db.Genres, "Id", "Name", albnm.GenreId);
            return View(albnm);
        }

        // GET: MVCStore/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Albnm albnm = db.Albnms.Find(id);
            if (albnm == null)
            {
                return HttpNotFound();
            }
            return View(albnm);
        }

        // POST: MVCStore/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Albnm albnm = db.Albnms.Find(id);
            db.Albnms.Remove(albnm);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
